package com.colin.library.help;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 字符串工具类
 *
 * @author Colin
 */
public class StringHelp {

    /**
     * View 控件上的文本内容
     *
     * @return
     */
    public static String getText(View view) {
        return getText(view, "");
    }


    /**
     * View 控件上的文本内容
     *
     * @param view
     * @param defaultValues
     * @return
     */
    public static String getText( View view, String defaultValues) {
        String view_string;
        if (view instanceof AutoCompleteTextView) {
            view_string = ((AutoCompleteTextView) view).getText().toString().trim();
        } else if (view instanceof EditText) {
            view_string = ((EditText) view).getText().toString().trim();
        } else if (view instanceof Button) {
            view_string = ((Button) view).getText().toString().trim();
        }  else if (view instanceof TextView) {
            view_string = ((TextView) view).getText().toString().trim();
        }else {
            view_string = "";
        }
        return StringHelp.isEmpty(view_string) ? defaultValues : view_string;
    }



    /**
     * 计算字数，一个汉字=两个英文字母，一个中文标点=两个英文标点 注意：该函数的不适用于对单个字符进行计算，因为单个字符四舍五入后都是1
     *
     * @param c
     * @return
     */
    public static long calculateLength(@NonNull CharSequence c) {
        double len = 0;
        for (int i = 0; i < c.length(); i++) {
            int tmp = (int) c.charAt(i);
            if (tmp > 0 && tmp < 127) {
                len += 0.5;
            } else {
                len++;
            }
        }
        return Math.round(len);
    }

    /**
     * 字符串转JSON
     *
     * @param json
     * @return
     * @throws JSONException
     */
    public static JSONObject toJSONObject(@NonNull String json) throws JSONException {
        if (!isEmpty(json)) {
            if (json.indexOf("[") == 0) {
                json = json.substring(1, json.length());
            }
            if (json.lastIndexOf("]") == json.length()) {
                json = json.substring(0, json.length() - 1);
            }
            return new JSONObject(json);
        }
        return null;
    }

    /**
     * 字符串转JSON
     *
     * @param json
     * @return
     * @throws JSONException
     */
    @NonNull
    public static JSONArray toJSONArray(String json) throws JSONException {
        return new JSONArray(json);
    }


    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(@Nullable String input) {
        return null == input || input.trim().length() == 0 || "".equals(input.trim()) || "null".equals(input.trim().toLowerCase());
    }

    /**
     * is null or its length is 0 or it is made by space
     * <p/>
     * <pre>
     * isBlank(null) = true;
     * isBlank(&quot;&quot;) = true;
     * isBlank(&quot;  &quot;) = true;
     * isBlank(&quot;a&quot;) = false;
     * isBlank(&quot;a &quot;) = false;
     * isBlank(&quot; a&quot;) = false;
     * isBlank(&quot;a b&quot;) = false;
     * </pre>
     *
     * @param str
     * @return if string is null or its size is 0 or it is made by space, return true, else return false.
     */
    public static boolean isBlank(String str) {
        return (str == null || str.trim().length() == 0);
    }

}
